package com.fa7.android.example.listafacil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	
	private Button addCategoria;
	private ListView mListView;
	private CompraAdapter mAdapter;
	private View mFooterView;
	private final String TOTAL_TEXT = "Total:";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mFooterView = getLayoutInflater().inflate(R.layout.footer_view, null);
		mListView = (ListView)findViewById(R.id.list_view);
		mListView.addFooterView(mFooterView);
		mAdapter = new CompraAdapter(this, 0);
		mListView.setAdapter(mAdapter);
		setTotalText();
		
		Button button = (Button)mFooterView.findViewById(R.id.footer_button);
		button.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void setTotalText() {
		TextView tv = (TextView)mFooterView.findViewById(R.id.footer_text);
		tv.setText(TOTAL_TEXT + " " + "0.00");
	}

	@Override
	public void onClick(View v) {
		Intent it = new Intent(this, AddActivity.class);
		startActivityForResult(it, 0);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.action_categoria:
				Intent it = new Intent(getApplicationContext(), AddCategoriaActivity.class);
				startActivity(it);
	            return true;
	    }
	    return true;
	}

}
